const dayjs = require("dayjs");

const {
    addJournalServer,
    queryJournalServer,
    updateJournalServer,
    queryAllJournalServer
} = require("../server/journalServer.js");
class journalController {
    async create(ctx, next) {
        const { title, content, plan_status, mood, user_id, happen, weather } =
            ctx.request.body;
        try {
            const data = await addJournalServer(
                title,
                content,
                plan_status,
                mood,
                user_id,
                happen,
                weather
            );
            ctx.body = {
                code: 0,
                message: "添加日记成功~",
                data,
            };
        } catch (error) {
            console.log(error);
        }
    }
    async queryJournalByTime(ctx, next) {
        const { now_time, status, user_id } = ctx.request.body;
        let startTime;
        let endTime;
        if (status === "day") {
            startTime = dayjs(now_time)
                .startOf("d")
                .format("YYYY-MM-DD HH:mm:ss");
            endTime = dayjs(now_time).endOf("d").format("YYYY-MM-DD HH:mm:ss");
        } else {
            startTime = dayjs(now_time)
                .startOf("w")
                .format("YYYY-MM-DD HH:mm:ss");
            endTime = dayjs(now_time).endOf("w").format("YYYY-MM-DD HH:mm:ss");
        }
        const data = await queryJournalServer(user_id, startTime, endTime);
        ctx.body = {
            code: 0,
            message: "查询日记成功~",
            data,
        };
    }

    async queryAllJournal(ctx,next){
        const {user_id , offset , limit} = ctx.request.body
        const data = await queryAllJournalServer(user_id ,  limit , offset)
        ctx.body = {
            code:0 ,
            message:"获取全部日记成功~",
            data
        }
    }

    async updateJournal(ctx,next){
        const { title, content, plan_status, mood,  happen, weather , journal_id } = ctx.request.body
        const data = await updateJournalServer(
            title,
            content,
            plan_status,
            mood,
            happen,
            weather,
            journal_id,
        );
        
        ctx.body = {
            code: 0,
            message: "修改日记成功~",
            data,
        };
    }
}

module.exports = new journalController();
